package com.hzw.saas.service.product.service;

import java.util.Date;
import java.util.Objects;

import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.IService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.hzw.saas.api.product.IPaymentOfflineService;
import com.hzw.saas.api.product.dto.PaymentOfflineDto;
import com.hzw.saas.common.config.exception.DefaultSaasException;
import com.hzw.saas.common.util.IdWorker;
import com.hzw.saas.service.product.mapper.PaymentOfflineMapper;
import com.hzw.saas.service.product.model.PaymentOffline;
import org.springframework.http.HttpStatus;
import org.springframework.stereotype.Service;
import lombok.RequiredArgsConstructor;
import ma.glasnost.orika.MapperFacade;

/**
 * @author zzl
 * @since 01/29/2021
 */
@Service("paymentOfflineServiceImpl")
@RequiredArgsConstructor
public class PaymentOfflineServiceImpl extends ServiceImpl<PaymentOfflineMapper, PaymentOffline> implements IPaymentOfflineService, IService<PaymentOffline> {

    private final IdWorker idWorker;
    private final MapperFacade mapperFacade;

    @Override
    public PaymentOfflineDto get(String payId) {
        if (StringUtils.isBlank(payId)) {
            return null;
        }
        PaymentOffline paymentOffline = this.getById(payId);
        return mapperFacade.map(paymentOffline, PaymentOfflineDto.class);
    }

    @Override
    public PaymentOfflineDto getByOrder(String orderId) {
        if (StringUtils.isBlank(orderId)) {
            return null;
        }
        PaymentOffline paymentOffline = this.lambdaQuery().eq(PaymentOffline::getOrderId, orderId).one();
        return mapperFacade.map(paymentOffline, PaymentOfflineDto.class);
    }

    @Override
    public PaymentOfflineDto add(PaymentOfflineDto paymentOfflineDto) {
        if (StringUtils.isBlank(paymentOfflineDto.getPayId())) {
            paymentOfflineDto.setPayId(String.valueOf(idWorker.nextId()));
        }
        if (Objects.isNull(paymentOfflineDto.getCreateTime())) {
            paymentOfflineDto.setCreateTime(new Date());
        }
        PaymentOffline paymentOffline = mapperFacade.map(paymentOfflineDto, PaymentOffline.class);
        this.save(paymentOffline);
        return paymentOfflineDto;
    }

    @Override
    public boolean update(PaymentOfflineDto paymentOfflineDto) {
        if (StringUtils.isBlank(paymentOfflineDto.getPayId())) {
            throw DefaultSaasException.build("支付ID为空").status(HttpStatus.BAD_REQUEST);
        }
        PaymentOffline paymentOffline = mapperFacade.map(paymentOfflineDto, PaymentOffline.class);
        return this.updateById(paymentOffline);
    }

    @Override
    public PaymentOfflineDto delete(String payId) {
        if (StringUtils.isBlank(payId)) {
            throw DefaultSaasException.build("支付ID为空").status(HttpStatus.BAD_REQUEST);
        }
        PaymentOfflineDto paymentOfflineDto = this.get(payId);
        boolean remove = this.removeById(payId);
        if (!remove) {
            throw DefaultSaasException.build("删除失败");
        }
        return paymentOfflineDto;
    }

    @Override
    public PaymentOfflineDto deleteByOrder(String orderId) {
        if (StringUtils.isBlank(orderId)) {
            throw DefaultSaasException.build("订单号为空").status(HttpStatus.BAD_REQUEST);
        }
        PaymentOfflineDto paymentOfflineDto = this.getByOrder(orderId);
        boolean remove = this.remove(Wrappers.<PaymentOffline>lambdaQuery().eq(PaymentOffline::getOrderId, orderId));
        if (!remove) {
            throw DefaultSaasException.build("删除失败");
        }
        return paymentOfflineDto;
    }
}
